package com.xea.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class TplUtil {
	
	private static String TPL_PATH = "d:/tpl/";
	
	private static Configuration cfg;
	
	public static Configuration getTelConfiguration() throws Exception{
		if(cfg==null){
			cfg = new Configuration();
			cfg.setDirectoryForTemplateLoading(new File(TPL_PATH));
			cfg.setObjectWrapper(new DefaultObjectWrapper());
		}
		return cfg;
	}
	
	public static void buildHTML(String ftlName,String htmlPath,Map data) throws Exception{
		cfg = getTelConfiguration();
		Template temp = cfg.getTemplate(ftlName);
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(htmlPath))));
		temp.process(data, out);
		out.flush();
	}
	
	
	
}
